package com.scalar.chiptrack.operations.reports.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.chiptrack.utils.ExcelUtils;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.reports.DataDumpReportsHelper;
import com.scalar.chiptrack.operations.reports.forms.PlanningReportForm;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: scalar soft
 * Date: Mar 23, 2007
 * Time: 6:12:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class PlanningReportAction extends ChipTrackAction {
     private static final String ACTION_NAME = "/planningReportAction.do";
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {

        String action = request.getParameter( "action" );
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        PlanningReportForm planningReportForm = (PlanningReportForm) form;
        ArrayList mktProductList   = null;
        ArrayList baseProductList   = null;
        ArrayList reportList   = null;
        Workbook workbook = null;
        String fileName = "";

        if ( action == null )
        {
            action = "";
        }
        try
        {
            if ( "planningReportAction".equalsIgnoreCase(action) || "".equals(action) )
            {
                String productId = planningReportForm.getProductId();//request.getParameter( "productId" );
                try
                {

                    baseProductList = NewProductDAO.loadAllBaseProducts( null, true, true );
                    mktProductList =  NewProductDAO.loadMarketingPartNumbersbyProductID( null, Constants.LOCATION_TYPE_FG, productId);
                }
                catch( Exception e )
                {
                    mktProductList     = new ArrayList();
                    baseProductList    = new ArrayList();

                    e.printStackTrace();
                }
                request.setAttribute( "mktProductList", mktProductList );
                request.setAttribute( "baseProductList", baseProductList );
            }
            else if ( "assmInstructionsReport".equalsIgnoreCase(action))
            {
                String productId = planningReportForm.getProductId();

                try
                {
                    baseProductList = NewProductDAO.loadAllBaseProducts( null, true, true );

                    mktProductList =  NewProductDAO.loadMarketingPartNumbersbyProductID( null, Constants.LOCATION_TYPE_FG, productId);
                }
                catch ( Exception e )
                {
                    baseProductList      = new ArrayList();
                    mktProductList       = new ArrayList();

                     e.printStackTrace();
                }

                request.setAttribute( "baseProductList", baseProductList );
                request.setAttribute( "mktProductList", mktProductList );
            }
            else
            {
                if ( "generatePlanningReportXls".equalsIgnoreCase(action) )
                {
                    String productId    = request.getParameter( "prompt0" );
                    String stepIds      = request.getParameter( "prompt1" );
                    String dateType   = request.getParameter( "prompt2" );
                    String category   = request.getParameter( "prompt3" );
                    String defaultPrompt= request.getParameter( "prompt4" );

                    reportList = NewProductDAO.loadPlanningReportDetails( null,productId,stepIds,dateType,category,defaultPrompt);

                    workbook =  DataDumpReportsHelper.getWorkbook( reportList, "Planning Report", null,null,null );
                    fileName = "PlanningReport_"+ DateUtil.getTimeStamp();
                }

                ExcelUtils.generateExcelReport( workbook, fileName, response );
            }
        }
        catch ( Exception e)
        {
            //errors.put ( "Could not generate excel file", null );
            e.printStackTrace();
        }

        if( "planningReportAction".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "planningReportAction" ) );
        }
        else if ( "assmInstructionsReport".equalsIgnoreCase(action) )
        {
            return ( mapping.findForward( "assmInstructionsReport" ) );
        }
        else
        {
            return null;
        }
    }
}
